package com.example.telesys.filters;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * 注意这是自定义的断言工厂
 * 1. 类的命名必须为 关键字+GatewayFilterFactory
 * 其中关键字必须对于配置文件Predicates数组中的【关键字】配置
 * 2. 并且必须集成AbstractXXX<配置类>
 */
@Component
public class LogGatewayFilterFactory extends AbstractGatewayFilterFactory<LogGatewayFilterFactory.Config> {

    public LogGatewayFilterFactory() {
        super(LogGatewayFilterFactory.Config.class);
    }

    //具体的过滤逻辑
    @Override
    public GatewayFilter apply(LogGatewayFilterFactory.Config config) {
        return (exchange, chain) -> {
            if (config.isLog()) {
                System.out.println("进入局部过滤器中...");
                System.out.println("检测到日志开启状态....");
            }
            return chain.filter(exchange);
        };
    }

    //赋值到Config配置类中的属性上
    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("log");
    }

    public static class Config {
        private boolean log;

        public Config(boolean log) {
            this.log = log;
        }

        public Config() {
        }

        public boolean isLog() {
            return log;
        }

        public void setLog(boolean log) {
            this.log = log;
        }
    }
}
